<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Definite assignment analysis</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Definite_assignment_analysis"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Definite_assignment_analysis rootpage-Definite_assignment_analysis skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Definite assignment analysis</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><p>In <a href="Computer_science" title="Computer science">computer science</a>, <b>definite assignment analysis</b> is a <a href="Data-flow_analysis" title="Data-flow analysis">data-flow analysis</a> used by <a href="Compiler" title="Compiler">compilers</a> to conservatively ensure that a variable or location is always assigned before it is used.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Motivation">Motivation</h2></div>
<p>In <a href="C_programming_language" class="mw-redirect" title="C programming language">C</a> and <a href="C%2B%2B" title="C++">C++</a> programs, a source of particularly difficult-to-diagnose errors is the nondeterministic behavior that results from reading <a href="Uninitialized_variable" title="Uninitialized variable">uninitialized variables</a>; this behavior can vary between platforms, builds, and even from run to run.
</p><p>There are two common ways to solve this problem. One is to ensure that all locations are written before they are read. <a href="Rice's_theorem" title="Rice's theorem">Rice's theorem</a> establishes that this problem cannot be solved in general for all programs; however, it is possible to create a conservative (imprecise) analysis that will accept only programs that satisfy this constraint, while rejecting some correct programs, and definite assignment analysis is such an analysis. The <a href="Java_programming_language" class="mw-redirect" title="Java programming language">Java</a><sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> and <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a><sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> programming language specifications require that the compiler report a compile-time error if the analysis fails. Both languages require a specific form of the analysis that is spelled out in meticulous detail. In Java, this analysis was formalized by Stärk et al.,<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> and some correct programs are rejected and must be altered to introduce explicit unnecessary assignments. In C#, this analysis was formalized by Fruja, and is precise as well as sound, in the sense that all variables assigned along all control flow paths will be considered definitely assigned.<sup id="cite_ref-fruja_4-0" class="reference"><a href="#cite_note-fruja-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> The <a href="Cyclone_(programming_language)" title="Cyclone (programming language)">Cyclone</a> language also requires programs to pass a definite assignment analysis, but only on variables with pointer types, to ease porting of C programs.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>
</p><p>The second way to solve the problem is to automatically initialize all locations to some fixed, predictable value at the point at which they are defined, but this introduces new assignments that may impede performance. In this case, definite assignment analysis enables a <a href="Compiler_optimization" class="mw-redirect" title="Compiler optimization">compiler optimization</a> where redundant assignments — assignments followed only by other assignments with no possible intervening reads — can be eliminated. In this case, no programs are rejected, but programs for which the analysis fails to recognize definite assignment may contain redundant initialization. The <a href="Common_Language_Infrastructure" title="Common Language Infrastructure">Common Language Infrastructure</a> relies on this approach.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Terminology">Terminology</h2></div>
<p>A variable or location can be said to be in one of three states at any given point in the program:
</p>
<ul><li><i>Definitely assigned</i>: The variable is known with certainty to be assigned.</li>
<li><i>Definitely unassigned</i>: The variable is known with certainty to be unassigned.</li>
<li><i>Unknown</i>: The variable may be assigned or unassigned; the analysis is not precise enough to determine which.</li></ul>
<div class="mw-heading mw-heading2"><h2 id="The_analysis">The analysis</h2></div>
<p>The following is based on Fruja's formalization of the C# intraprocedural (single method) definite assignment analysis, which is responsible for ensuring that all local variables are assigned before they are used.<sup id="cite_ref-fruja_4-1" class="reference"><a href="#cite_note-fruja-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> It simultaneously does definite assignment analysis and <a href="Constant_propagation" class="mw-redirect" title="Constant propagation">constant propagation</a> of boolean values. We define five static functions:
</p>
<table class="wikitable">
<tbody><tr>
<th>Name</th>
<th>Domain</th>
<th>Description
</th></tr>
<tr>
<td><i>before</i></td>
<td>All statements and expressions</td>
<td>Variables definitely assigned before the evaluation of the given statement or expression.
</td></tr>
<tr>
<td><i>after</i></td>
<td>All statements and expressions</td>
<td>Variables definitely assigned after the evaluation of the given statement or expression, assuming it completes normally.
</td></tr>
<tr>
<td><i>vars</i></td>
<td>All statements and expressions</td>
<td>All variables available in the scope of the given statement or expression.
</td></tr>
<tr>
<td><i>true</i></td>
<td>All boolean expressions</td>
<td>Variables definitely assigned after the evaluation of the given expression, assuming the expression evaluates to <b>true</b>.
</td></tr>
<tr>
<td><i>false</i></td>
<td>All boolean expressions</td>
<td>Variables definitely assigned after the evaluation of the given expression, assuming the expression evaluates to <b>false</b>.
</td></tr></tbody></table>
<p>We supply data-flow equations that define the values of these functions on various expressions and statements, in terms of the values of the functions on their syntactic subexpressions. Assume for the moment that there are no <i>goto</i>, <i>break</i>, <i>continue</i>, <i>return</i>, or <a href="Exception_handling" title="Exception handling">exception handling</a> statements. Following are a few examples of these equations:
</p>
<ul><li>Any expression or statement <i>e</i> that does not affect the set of variables definitely assigned: <i>after</i>(<i>e</i>) = <i>before</i>(<i>e</i>)</li>
<li>Let <i>e</i> be the assignment expression <i>loc</i> = <i>v</i>. Then <i>before</i>(<i>v</i>) = <i>before</i>(<i>e</i>), and <i>after</i>(<i>e</i>) = <i>after</i>(<i>v</i>) U {loc}.</li>
<li>Let <i>e</i> be the expression <b>true</b>. Then <i>true</i>(<i>e</i>) = <i>before</i>(<i>e</i>) and <i>false</i>(<i>e</i>) = <i>vars</i>(<i>e</i>). In other words, if <i>e</i> evaluates to <b>false</b>, all variables are (<a href="Vacuous_truth" title="Vacuous truth">vacuously</a>) definitely assigned, because <i>e</i> does not evaluate to false.</li>
<li>Since method arguments are evaluated left to right, before(<i>arg</i><sub><i>i</i> + 1</sub>) = after(<i>arg</i><sub><i>i</i></sub>). After a method completes, <i>out</i> parameters are definitely assigned.</li>
<li>Let <i>s</i> be the conditional statement <b>if</b> (<i>e</i>) <i>s</i><sub>1</sub> <b>else</b> <i>s</i><sub>2</sub>. Then <i>before</i>(<i>e</i>) = <i>before</i>(<i>s</i>), <i>before</i>(s<sub>1</sub>) = <i>true</i>(<i>e</i>), <i>before</i>(<i>s</i><sub>2</sub>) = <i>false</i>(<i>e</i>), and after(<i>s</i>) = after(<i>s</i><sub>1</sub>) intersect after(<i>s</i><sub>2</sub>).</li>
<li>Let <i>s</i> be the while loop statement <b>while</b> (<i>e</i>) <i>s</i><sub>1</sub>. Then before(<i>e</i>) = before(<i>s</i>), before(<i>s</i><sub>1</sub>) = true(<i>e</i>), and after(<i>s</i>) = false(<i>e</i>).</li>
<li>And so on.</li></ul>
<p>At the beginning of the method, no local variables are definitely assigned. The verifier repeatedly iterates over the <a href="Abstract_syntax_tree" title="Abstract syntax tree">abstract syntax tree</a> and uses the data-flow equations to migrate information between the sets until a <a href="Fixed_point_(mathematics)" title="Fixed point (mathematics)">fixed point</a> can be reached. Then, the verifier examines the <i>before</i> set of every expression that uses a local variable to ensure that it contains that variable.
</p><p>The algorithm is complicated by the introduction of control-flow jumps like <i>goto</i>, <i>break</i>, <i>continue</i>, <i>return</i>, and exception handling. Any statement that can be the target of one of these jumps must intersect its <i>before</i> set with the set of definitely assigned variables at the jump source. When these are introduced, the resulting data flow may have multiple fixed points, as in this example:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr mw-highlight-lines" dir="ltr"><pre><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="nl">L</span><span class="p">:</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">L</span><span class="p">;</span>
</pre></div>
<p>Since the label L can be reached from two locations, the control-flow equation for goto dictates that <i>before</i>(2) = <i>after</i>(1) intersect <i>before</i>(3). But <i>before</i>(3) = <i>before</i>(2), so <i>before</i>(2) = <i>after</i>(1) intersect <i>before</i>(2). This has two fixed-points for <i>before</i>(2), {i} and the empty set. However, it can be shown that because of the monotonic form of the data-flow equations, there is a unique maximal fixed point (fixed point of largest size) that provides the most possible information about the definitely assigned variables. Such a maximal (or maximum) fixed point may be computed by standard techniques; see <a href="Data-flow_analysis" title="Data-flow analysis">data-flow analysis</a>.
</p><p>An additional issue is that a control-flow jump may render certain control flows infeasible; for example, in this code fragment the variable <i>i</i> is definitely assigned before it is used:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr mw-highlight-lines" dir="ltr"><pre><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="k">return</span><span class="p">;</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span>
<span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
</pre></div>
<p>The data-flow equation for <i>if</i> says that <i>after</i>(2) = after(<b>return</b>) intersect after(<i>i</i> = <i>j</i>). To make this work out correctly, we define <i>after</i>(<i>e</i>) = <i>vars</i>(<i>e</i>) for all control-flow jumps; this is vacuously valid in the same sense that the equation <i>false</i>(<b>true</b>) = <i>vars</i>(<i>e</i>) is valid, because it is not possible for control to reach a point immediately after a control-flow jump.
</p>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFJ._GoslingB._JoyG._SteeleG._Bracha" class="citation web cs1">J. Gosling; B. Joy; G. Steele; G. Bracha. <a rel="nofollow" class="external text" href="http://java.sun.com/docs/books/jls/third_edition/html/defAssign.html">"The Java Language Specification, 3rd Edition"</a>. pp. Chapter 16 (pp.527–552)<span class="reference-accessdate">. Retrieved <span class="nowrap">December 2,</span> 2008</span>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.ecma-international.org/publications/standards/Ecma-334.htm">"Standard ECMA-334, C# Language Specification"</a>. <i>ECMA International</i>. pp. Section 12.3 (pp.122–133)<span class="reference-accessdate">. Retrieved <span class="nowrap">December 2,</span> 2008</span>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite id="CITEREFStärkE._BorgerJoachim_Schmid2001" class="citation book cs1">Stärk, Robert F.; E. Borger; Joachim Schmid (2001). <i>Java and the Java Virtual Machine: Definition, Verification, Validation</i>. Secaucus, NJ, USA: Springer-Verlag New York, Inc. pp. Section 8.3. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>3-540-42088-6</bdi>.</cite></span>
</li>
<li id="cite_note-fruja-4"><span class="mw-cite-backlink">^ <a href="#cite_ref-fruja_4-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-fruja_4-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite id="CITEREFFruja2004" class="citation journal cs1">Fruja, Nicu G. (October 2004). <a rel="nofollow" class="external text" href="http://www.jot.fm/issues/issue_2004_10/article2">"The Correctness of the Definite Assignment Analysis in C#"</a>. <i>Journal of Object Technology</i>. <b>3</b> (9): <span class="nowrap">29–</span>52. <a href="CiteSeerX_(identifier)" class="mw-redirect" title="CiteSeerX (identifier)">CiteSeerX</a> <span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.165.6696">10.1.1.165.6696</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.5381%2Fjot.2004.3.9.a2">10.5381/jot.2004.3.9.a2</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2008-12-02</span></span>. <q>We actually prove more than correctness: we show that the solution of the analysis is a perfect solution (and not only a safe approximation).</q></cite></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://cyclone.thelanguage.org/wiki/Definite%20Assignment">"Cyclone: Definite Assignment"</a>. <i>Cyclone User's Manual</i><span class="reference-accessdate">. Retrieved <span class="nowrap">December 16,</span> 2008</span>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.ecma-international.org/publications/standards/Ecma-335.htm">"Standard ECMA-335, Common Language Infrastructure (CLI)"</a>. <i>ECMA International</i>. pp. Section 1.8.1.1 (Partition III, pg. 19)<span class="reference-accessdate">. Retrieved <span class="nowrap">December 2,</span> 2008</span>.</cite></span>
</li>
</ol></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2020-05-11" href="https://en.wikipedia.org/wiki/?title=Definite_assignment_analysis&oldid=956133309">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>